package com.simon.batteryinformation;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;

class DataAdapter extends ResourceCursorAdapter {
	private final String TAG = "DataAdapter";

	private Map<String, Boolean> conf;

	public DataAdapter(Context context, Cursor cur, Map<String, Boolean> conf) {
		super(context, R.layout.showdata, cur);
		this.conf = conf;
	}

	/**
	 * Show data from database.
	 * 
	 * @param view
	 *            View -
	 * @param context
	 *            Context -
	 * @param cursor
	 *            Cursor - cursor with data.
	 */
	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		String volt = cursor.getString(cursor.getColumnIndex("volt"));
		Calendar cal = Calendar.getInstance();
		Integer level = cursor.getInt(cursor.getColumnIndex("level"));
		String health = cursor.getString(cursor.getColumnIndex("health"));
		String temp = cursor.getString(cursor.getColumnIndex("temp"));

		cal.setTimeInMillis(cursor.getLong(cursor.getColumnIndex("time")));
		String format = "dd/MM/y h:mm a";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String dateString = sdf.format(cal.getTime());

		if (!conf.get("id")) {
			((TextView) view.findViewById(R.id.id)).setVisibility(View.GONE);
		}
		if (!conf.get("date")) {
			((TextView) view.findViewById(R.id.date)).setVisibility(View.GONE);
		}
		if (!conf.get("volt")) {
			((TextView) view.findViewById(R.id.volt)).setVisibility(View.GONE);
		}
		if (!conf.get("health")) {
			((TextView) view.findViewById(R.id.health))
					.setVisibility(View.GONE);
		}
		if (!conf.get("level")) {
			((TextView) view.findViewById(R.id.level)).setVisibility(View.GONE);
		}
		if (!conf.get("temp")) {
			((TextView) view.findViewById(R.id.temp)).setVisibility(View.GONE);
		}

		((TextView) view.findViewById(R.id.date)).setText(dateString);
		((TextView) view.findViewById(R.id.volt)).setText(volt);
		((TextView) view.findViewById(R.id.health)).setText(health);
		((TextView) view.findViewById(R.id.level)).setText(level + "");
		((TextView) view.findViewById(R.id.temp)).setText(temp + "");
		// Log.v(TAG, "GONE");

	}
}